package com.qfhd.soft.cjzcore.TaskCenter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.widget.FrameLayout;

import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.qfhd.soft.cjzcore.Core.CJLogger;
import com.qfhd.soft.cjzcore.Listener.CJAdListener;
import com.qfhd.soft.cjzcore.Listener.CJLoadFinishListener;
import com.qfhd.soft.cjzcore.Listener.CJRetListListener;
import com.qfhd.soft.cjzcore.TaskCenter.ADModel.ADModel;
import com.qfhd.soft.cjzcore.TaskCenter.ADModel.ADResourceModel;
import com.qfhd.soft.cjzcore.TaskCenter.CSJ.CJCSJsdk;
import com.qfhd.soft.cjzcore.TaskCenter.GDT.CJGDTsdk;
import com.qq.e.ads.nativ.NativeExpressADView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CJAdCenter {

    public static final int AD_TYPE_GDT = 4402;
    public static final int AD_TYPE_CSJ = 4401;

    public static final int AD_Place_screen = 5501;//开屏
    public static final int AD_Place_ggk_video = 1101;//刮刮卡-激励视频
    public static final int AD_Place_ggk_navi = 1102;//刮刮卡-信息流
    public static final int AD_Place_ggk_Dialoig_navi = 1103;//刮刮卡-弹窗信息流
    public static final int AD_Place_list = 2201;//列表信息流

    public static final int Type_Splash = 3301;
    public static final int Type_Video = 3302;
    public static final int Type_Navi = 3303;
    public static final int Type_List = 4;

    public static int List_Navi_Size      = 10;

    public static final int AD_Status_Click    = 1;
    public static final int AD_Status_Download = 2;
    public static final int AD_Status_Install  = 3;
    public static final int AD_Status_Close    = 4;
    public static final int AD_Status_Reward   = 5;//给予奖励
    public static final int AD_Status_HideDialog = 6; //广点通 视频广告 点击时 不隐藏弹窗的话，弹窗在视频上层
    public static final int AD_Status_ShowDialog = 7;
    public static final int AD_Status_OpenFrame  = 8;//展开frame
    public static final int AD_Status_CloseFrame = 9;//打开frame

    private ArrayList<ADModel>adInfos = new ArrayList<ADModel>();

    private static CJAdCenter adCenter;

    public static CJAdCenter getInstance() {
        if (adCenter == null) {
            synchronized (CJAdCenter.class) {
                adCenter = new CJAdCenter();
            }
        }
        return adCenter;
    }

    public void initAd(Context context){
        CJCSJsdk.getInstance().init(context);
        CJGDTsdk.getInstance().init(context);
    }

    public void setAdInfos(JSONArray array){
        adInfos.clear();
        for(int i=0; i<array.length(); i++){
            try {
                JSONObject obj = array.getJSONObject(i);
                ADModel model = ADModel.getADModelByJSONObject(obj);
                CJLogger.v("adinfo:" + model.toString());
                adInfos.add(model);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        //根据数据 给广告源的广告位设置 id
        for(ADModel item:adInfos){
            switch (item.ggStyle){
                case Type_Splash:{
                    for(ADResourceModel resourceModel:item.resources){
                        if(resourceModel.ggResource == AD_TYPE_CSJ){
                            CJCSJsdk.getInstance().setSplashId(resourceModel.ggID);
                        }
                        if(resourceModel.ggResource == AD_TYPE_GDT){
                            CJGDTsdk.getInstance().setSplashId(resourceModel.ggID);
                        }
                    }
                }
                break;
                case Type_Video:{
                    for(ADResourceModel resourceModel:item.resources){
                        if(resourceModel.ggResource == AD_TYPE_CSJ){
                            CJCSJsdk.getInstance().setRewardVideoId(resourceModel.ggID);
                        }
                        if(resourceModel.ggResource == AD_TYPE_GDT){
                            CJGDTsdk.getInstance().setRewardVideoId(resourceModel.ggID);
                        }
                    }
                }
                break;
                case Type_Navi:{
                    for(ADResourceModel resourceModel:item.resources){
                        if(resourceModel.ggResource == AD_TYPE_CSJ){
                            CJCSJsdk.getInstance().setNaviAdId(resourceModel.ggID);
                        }
                        if(resourceModel.ggResource == AD_TYPE_GDT){
                            CJGDTsdk.getInstance().setNaviAdId(resourceModel.ggID);
                        }
                    }
                }
                break;
                default:
                    break;
            }
        }
    }

    public void setAPPID_GDT(String appID){
        CJGDTsdk.getInstance().setAPPID(appID);
    }

    public void setAPPID_CSJ(String appID){
        CJCSJsdk.getInstance().setAPPID(appID);
    }

    public boolean appIdIsGetter(){
        if(!TextUtils.isEmpty(CJCSJsdk.APPID) && !TextUtils.isEmpty(CJGDTsdk.APPID)){
            return true;
        }
        else {
            return false;
        }
    }

    private int getAdSource(int ggPlace){
        //通过广告位，判断取哪种广告源
        int ggResourceType = ADModel.getResourceTypeFromModel(ADModel.getModelByGGplace(adInfos,ggPlace));
//        CJLogger.v("getAdSource:" + ggResourceType);
        if(ggResourceType == 0){
            CJLogger.v("something error,get default resoureType  ggPlace:" + ggPlace);
            return AD_TYPE_GDT;
        }
        else{
            return ggResourceType;
        }
    }

    private int getAdType(int ggPlace){
        int sourceType = getAdSource(ggPlace);
        switch (sourceType){
            case AD_TYPE_CSJ:{
                CJLogger.v("AD resource Type : CSJ" );
            }
            break;
            case AD_TYPE_GDT:{
                CJLogger.v("AD resource Type : GDT" );
            }
            break;
            default:
                CJLogger.v("AD resource Type : null" );
                break;
        }
        return sourceType;
    }

    public void showSplashAd(int ggPlace,Activity activity, FrameLayout adContainer, CJLoadFinishListener overListener){
        new Thread(()->{
            switch (getAdType(ggPlace)){
                case AD_TYPE_CSJ:{
                    activity.runOnUiThread(()->{
                        CJCSJsdk.getInstance().showSplash(activity,adContainer,overListener);
                    });
                }
                break;
                case AD_TYPE_GDT:{
                    activity.runOnUiThread(()->{
                        CJGDTsdk.showSplashAd(activity,adContainer,overListener);
                    });
                }
                break;
                default:
                    break;
            }
        }).start();
    }

    public void showRewardVidewo(int ggPlace,Activity activity, Context context, CJAdListener statusListener){
        new Thread(()->{
            switch (getAdType(ggPlace)){
                case AD_TYPE_CSJ:{
                    activity.runOnUiThread(()->{
                        CJCSJsdk.getInstance().showRewardVidewo(activity,context,statusListener);
                    });
                }
                break;
                case AD_TYPE_GDT:{
                    activity.runOnUiThread(()->{
                        CJGDTsdk.showRewardVideo(activity,context,statusListener);
                    });
                }
                break;
                default:
                    break;
            }
        }).start();
    }

    public void showNavi(int ggPlace,Context context, int width, int height, FrameLayout mExpressContainer, Activity activity, CJAdListener statusListener){
        new Thread(()->{
            switch (getAdType(ggPlace)){
                case AD_TYPE_CSJ:{
                    activity.runOnUiThread(()->{
                        CJCSJsdk.getInstance().showNavi(context,width,height,mExpressContainer,activity,statusListener);
                    });
                }
                break;
                case AD_TYPE_GDT:{
                    activity.runOnUiThread(()->{
                        CJGDTsdk.showNavi(context,width,height,mExpressContainer,activity,statusListener);
                    });
                }
                break;
                default:
                    break;
            }
        }).start();
    }

    public void showNavi_dialog(int ggPlace,Context context, int width, int height, FrameLayout mExpressContainer, Activity activity, CJAdListener statusListener){
        new Thread(()->{
            switch (getAdType(ggPlace)){
                case AD_TYPE_CSJ:{
                    activity.runOnUiThread(()->{
                        CJCSJsdk.getInstance().showNavi_dialog(context,width,height,mExpressContainer,activity,statusListener);
                    });
                }
                break;
                case AD_TYPE_GDT:{
                    activity.runOnUiThread(()->{
                        CJGDTsdk.showNavi_dialog(context,width,height,mExpressContainer,activity,statusListener);
                    });
                }
                break;
                default:
                    break;
            }
        }).start();
    }

    public void getListNaviAds(int ggPlace,Context context, int width, int height, CJRetListListener retListener, CJAdListener adListener){

//        //由于CSJ 一次最多只能取3条广告
//        List<Visitable> csj_list = new ArrayList<>();
//        CJRetListListener csj_listener = new CJRetListListener() {
//            @Override
//            public void returnList(Object object) {
//                List<Visitable> tmp = (List<Visitable>)object;
//                csj_list.addAll(tmp);
//                if(csj_list.size() >= adCenter.List_Navi_Size){
//                    retListener.returnList(csj_list);
//                }
//            }
//        };

        new Thread(()->{
            switch (getAdType(ggPlace)){
                case AD_TYPE_CSJ:{
                    int count = adCenter.List_Navi_Size / 3;
                    int yu = adCenter.List_Navi_Size % 3;
                    if(yu > 0){
                        count++;
                    }
                    for(int i=0 ; i<count+1 ;i++){
                        CJCSJsdk.getInstance().getListNaviAd(context,width,height,retListener,adListener);
                    }
                }
                break;
                case AD_TYPE_GDT:{
                    CJGDTsdk.getListNaviAd(context,width,height,retListener,adListener);
                }
                break;
                default:
                    break;
            }
        }).start();
    }
}
